// login.js
var ajax = require("../../utils/network.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    byMessageTime: '获取验证码',
    buttonDisable: false,
    mobile: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  //保存输入的账号
  mobileInputEvent: function (e) {
    this.setData({
      mobile: e.detail.value
    })
  },

  //短信登陆
  formSubmit: function (e) {
    var that = this;
    var mobile = e.detail.value.mobile;
    var message = e.detail.value.message;
    if (mobile == '') {
      wx.showModal({
        content: '请输入手机号码',
        showCancel: false,
        confirmColor: '#12bbcd'
      })
      return false
    }
    if (message == '') {
      wx.showModal({
        content: '请输入验证码',
        showCancel: false,
        confirmColor: '#12bbcd'
      })
      return false
    }
    var URL = ajax.server() + 'member/login-by-other';
    var Params = { mobile: mobile, code: message };
    var Method = 'POST';
    var Message = '';
    ajax.requestPost(URL, Params, Method, Message, function (res) {
      console.log('短信登陆---', res);
      if (res.status == true) {
        wx.navigateBack();
        wx.showToast({
          title: res.msg,
        })
        wx.setStorageSync('mobile', res.data.mobile);
        wx.setStorageSync('token', res.data.token);
      }
      else if (res.false == -1) {
        if (res.status == false) {
          wx.showToast({
            title: res.msg,
          })
        }
      }
    }, function () {})
  },


  //获取验证码
  byMessage: function (e) {
    var that = this;
    var regMobile = /^1[34578]\d{9}$/;
    var mobile = that.data.mobile;
    if (that.data.buttonDisable) return false;
    if (!regMobile.test(mobile)) {
      wx.showToast({
        title: '手机号码有误！'
      })
      return false;
    }
    else {
      var c = 60;
      var intervalId = setInterval(function () {
        c = c - 1;
        that.setData({
          byMessageTime: c + 's后重发',
          buttonDisable: true
        })
        if (c == 0) {
          clearInterval(intervalId);
          that.setData({
            byMessageTime: '获取验证码',
            buttonDisable: false
          })
        }
      }, 1000)
      var URL = ajax.server() + 'sms/user-register';
      var Params = { mobile: mobile };
      var Method = 'POST';
      var Message = '';
      ajax.requestPost(URL, Params, Method, Message, function (res) {
        console.log('验证码---', res);
        if (res.status == true) {
          wx.showToast({
            title: res.msg,
          })
        }
        if (res.status == false) {
          wx.showToast({
            title: res.msg,
          })
        }
      }, function () {})
    }
  },
})